package cn.monkey.family.server.domain.uc.user.role;

import cn.monkey.family.data.crud.repository.CrudRepository;
import cn.monkey.family.data.uc.user.role.QUserRole;
import cn.monkey.family.data.uc.user.role.UserRole;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.querydsl.ListQuerydslPredicateExecutor;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.query.ListQueryByExampleExecutor;

import java.util.Collection;

@NoRepositoryBean
public interface UserRoleRepository extends CrudRepository<UserRole, String>, ListQueryByExampleExecutor<UserRole>, ListQuerydslPredicateExecutor<UserRole> {

    default Collection<UserRole> findByRoleId(String roleId) {
        UserRole userRole = new UserRole();
        userRole.setRoleId(roleId);
        return findAll(Example.of(userRole, ExampleMatcher.matchingAll().withMatcher("roleId", ExampleMatcher.GenericPropertyMatchers.exact())));
    }

    default Collection<UserRole> findByUid(String uid) {
        UserRole userRole = new UserRole();
        userRole.setUid(uid);
        return findAll(Example.of(userRole, ExampleMatcher.matchingAll().withMatcher("uid", ExampleMatcher.GenericPropertyMatchers.exact())));
    }

    default Collection<UserRole> findByUids(Collection<String> uids) {
        QUserRole userRole = QUserRole.userRole;
        return findAll(userRole.uid.in(uids));
    }
}
